package com.example.wu5hen.n0te;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.*;
import android.widget.TextView;


import com.example.wu5hen.n0te.*;

public class Adapter extends RecyclerViewCursorAdapter<Adapter.DiaryHolder>{
    private LayoutInflater inflater;

    public static interface OnRecyclerViewListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }
    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener mOnItemClickListener){
        this.onRecyclerViewListener=mOnItemClickListener;
    }

    public Adapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        inflater=LayoutInflater.from(context);
    }
    

    @Override
    public void onBindViewHolder(final DiaryHolder holder, Cursor cursor) {
        Diary diary=new Diary();
        diary.setTitle(cursor.getString(cursor.getColumnIndex(Fields.DiaryColumns.TITLE)));
        diary.setBody(cursor.getString(cursor.getColumnIndex(Fields.DiaryColumns.BODY)));
        diary.setTime(cursor.getString(cursor.getColumnIndex(Fields.DiaryColumns.CREATED)));

        holder.title.setText(diary.getTitle());
        holder.body.setText(diary.getBody());
        holder.time.setText(diary.getTime());



        if(onRecyclerViewListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    onRecyclerViewListener.onItemClick(holder.itemView,pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos=holder.getLayoutPosition();
                    onRecyclerViewListener.onItemLongClick(holder.itemView,pos);
                    return false;
                }
            });
        }
    }

    @Override
    protected void onContentChanged() {

    }
    @Override
    public DiaryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=inflater.inflate(R.layout.card_row,parent,false);
        final DiaryHolder holder=new DiaryHolder(v);


        return holder;
    }
    class DiaryHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public TextView body;
        public TextView time;
        View view;

        public DiaryHolder(View itemView) {
            super(itemView);
            view=itemView;
            title=(TextView) itemView.findViewById(R.id.dairy_title);
            body=(TextView) itemView.findViewById(R.id.dairy_body);
            time=(TextView) itemView.findViewById(R.id.dairy_time);
        }
    }
}
